package com.njitzx.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.njitzx.constant.MemberConstant;
import com.njitzx.entity.PageMemberVO;
import com.njitzx.entity.po.Member;
import com.njitzx.entity.vo.MemberVO;
import com.njitzx.entity.vo.SpMemberVO;
import com.njitzx.mapper.MemberMapper;
import com.njitzx.service.MemberService;
import com.njitzx.util.PwdUtil;
import lombok.SneakyThrows;
//import org.apache.poi.ss.usermodel.Workbook;
//import org.apache.poi.xssf.usermodel.XSSFRow;
//import org.apache.poi.xssf.usermodel.XSSFSheet;
//import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.sound.midi.Soundbank;


/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 周鑫
 * @since 2024-08-20
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class MemberServiceImpl implements MemberService {

    private final MemberMapper memberMapper;

    /**
     * 会员表分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param member
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageMemberVO findPage(Integer pageNum, Integer pageSize, Member member) {
        //分页
        PageHelper.startPage(pageNum, pageSize);
        List<MemberVO> memberList = memberMapper.selectVO(member);
        PageInfo<MemberVO> memberPageInfo = new PageInfo<>(memberList);

        PageMemberVO pageMemberVO=new PageMemberVO();
        pageMemberVO.setMembers(memberPageInfo.getList());
        pageMemberVO.setPageNum(pageNum);
        pageMemberVO.setTotal((int) memberPageInfo.getTotal());
        return pageMemberVO;
    }

    /**
     * 根据主键查询会员表
     *
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public MemberVO findById(Serializable id) {
        return memberMapper.selectById(id);
    }

    /**
     * 会员表新增
     *
     * @param member
     */
    @Override
    public void add(Member member) {
        String username = member.getUsername();
        Member member1 = memberMapper.selectByUername(username);
        if (member1 != null) {
            throw new RuntimeException("用户名已存在");
        }
        member.setPassword(PwdUtil.encrypt(member.getPassword()));
        member.setCreateTime(new Date());
        member.setUpdateTime(new Date());
        member.setIsDeleted(MemberConstant.NO_DELETED);
        memberMapper.insert(member);
    }

    /**
     * 修改会员表
     *
     * @param member
     */
    @Override
    public void modify(Member member) {
        memberMapper.update(member);
    }

    @Override
    public void removeByIds(List<Integer> ids) {
        memberMapper.deleteByIds(ids);
    }

    @SneakyThrows
    @Override
    public void importFile(InputStream inputStream){
        System.out.println(inputStream);
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);                              //这边读取的是vo中的配置
        List<SpMemberVO> list = ExcelImportUtil.importExcel(inputStream, SpMemberVO.class, params);
        System.out.println(list);
        List<Member> collect = list.stream().map(item -> {
            Member member = new Member();
            BeanUtils.copyProperties(item, member);
            member.setPassword(PwdUtil.encrypt(MemberConstant.DEFAULT_PASSWORD));
            member.setCreateTime(new Date());
            member.setUpdateTime(new Date());
            member.setIsDeleted(MemberConstant.NO_DELETED);
            return member;
        }).collect(Collectors.toList());
        memberMapper.insertBatch(collect);
    }


//    public void importFile(File tempFile) throws IOException {
//        FileInputStream inputStream = new FileInputStream(tempFile);
//        //创建一个工作部
//        XSSFWorkbook excel = new XSSFWorkbook(inputStream);
//
//        //拿到第一张工作表
//        XSSFSheet sheet = excel.getSheetAt(0);
//        int lastRowNum = sheet.getLastRowNum();
//        //创建member对象
//        List<Member> list = new ArrayList<>();
//
//        for (int i = 2; i <= lastRowNum; i++) {
//            Member member = new Member();
//            XSSFRow row = sheet.getRow(i); //拿到行
//            String username = row.getCell(0).getStringCellValue();
//            String email = row.getCell(1).getStringCellValue();
//            String sex = row.getCell(2).getStringCellValue();
//            String qq = row.getCell(3).getStringCellValue();
//            String phone = row.getCell(4).getStringCellValue();
//            String education = row.getCell(5).getStringCellValue();
//            String hobby = row.getCell(6).getStringCellValue();
//            String descp = row.getCell(7).getStringCellValue();
//
//            member.setUsername(username);
//            member.setEmail(email);
//            member.setGender(sex);
//            member.setQq(qq);
//            member.setPhone(phone);
//            member.setEducation(education);
//            member.setHobby(hobby);
//            member.setIntroduce(descp);
//            //密码加密 默认密码是123
//            member.setPassword(PwdUtil.encrypt(MemberConstant.DEFAULT_PASSWORD));
//            member.setCreateTime(new Date());
//            member.setUpdateTime(new Date());
//            //setIsDeleted
//            member.setIsDeleted(MemberConstant.NO_DELETED);
//            list.add(member);
//        }
//        list.forEach(System.out::println);
//        memberMapper.insertBatch(list);
//    }

    @Override
    public Workbook export(Integer pageNum, Integer pageSize, Member member) {
        PageHelper.startPage(pageNum, pageSize);
        List<SpMemberVO> memberList = memberMapper.selectByPage(member);
        ExportParams exportParams = new ExportParams("会员信息", "会员信息表");

        Workbook book = ExcelExportUtil.exportExcel(exportParams, SpMemberVO.class, memberList);

        return book;
    }

    /**
     * 根据主键删除会员表
     *
     * @param id
     */
    @Override
    public void removeById(Integer id) {
        memberMapper.deleteById(id);
    }
}
